package com.gee.spring.cloud.data.transform.core.center.config.handler;

import com.gee.spring.cloud.common.core.api.result.ApiResult;
import com.gee.spring.cloud.common.core.entity.center.config.FrameMapping;
import com.gee.spring.cloud.common.starter.config.event.CenterConfigEventHandler;
import com.gee.spring.cloud.common.starter.feign.center.config.FrameMappingClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;

import static cn.hutool.core.text.StrPool.UNDERLINE;

/**
 * @author gepengjun
 * @since 2024/3/19 14:31
 */
@Component
public class FrameMappingHandler extends CenterConfigEventHandler<FrameMapping> {

    //key: orgId_mappingFrameId
    private final ConcurrentHashMap<String, FrameMapping> frameMappingMap = new ConcurrentHashMap<>();

    @Autowired
    private FrameMappingClient frameMappingClient;

    public FrameMapping getByOrgIdAndMappingFrameId(Long orgId, Long frameId) {
        String key = buildKey(orgId, frameId);
        return frameMappingMap.computeIfAbsent(key, newKey -> frameMappingClient.getByOrgIdAndFrameId(orgId, frameId).getData());
    }

    public FrameMapping getByOrgIdAndMappingFrameBid(Long orgId, Long mappingFrameBid) {
        Optional<FrameMapping> frameMappingOptional = frameMappingMap.values().stream()
                .filter(frameMapping -> frameMapping.getMappingFrameBid() == mappingFrameBid)
                .findFirst();
        if (frameMappingOptional.isPresent()){
            return frameMappingOptional.get();
        }else {
            ApiResult<FrameMapping> frameMappingApiResult = frameMappingClient.getByOrgIdAndMappingFrameBid(orgId, mappingFrameBid);
            FrameMapping frameMapping = frameMappingApiResult.getData();
            if (frameMappingApiResult.isSuccess() && frameMapping != null){
                String key = buildKey(frameMapping.getOrgId(), frameMapping.getFrameId());
                frameMappingMap.put(key, frameMapping);
                return frameMapping;
            }else {
                return null;
            }
        }
    }

    @Override
    public void handlerUpdate(List<FrameMapping> dataList, List<FrameMapping> oldList) {
        dataList.forEach(frameMapping ->{
            String key = buildKey(frameMapping.getOrgId(), frameMapping.getFrameId());
            frameMappingMap.computeIfPresent(key, (oldKey, old) -> frameMapping);
        });
    }

    @Override
    public void handlerDelete(List<FrameMapping> oldList) {
        oldList.forEach(frameMapping -> {
            String key = buildKey(frameMapping.getOrgId(), frameMapping.getFrameId());
            frameMappingMap.remove(key);
        });
    }

    private static String buildKey(Long orgId, Long mappingFrameId){
        return new StringJoiner(UNDERLINE).add(orgId.toString()).add(mappingFrameId.toString()).toString();
    }
}
